<?php

session_start();

//Encodage des caractères
header('Content-Type: text/html; charset=$ENCODAGE');

const AUCUN_RESULTAT = "<div style='text-align:center; font-size:11pt; margin:5px; '><span style='color:white;'>Aucun r&eacute;sultat</span>\n</div>\n";

//Librairie de fonctions
require("lib/libencode.inc.php");
require("lib/libmysql.inc.php");

setConnectionSql();

function enleveAccent($chaine) {
   $accent = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿ";
   $noaccent = "AAAAAAACEEEEIIIIDNOOOOOOUUUUYBSaaaaaaaceeeeiiiidnoooooouuuyyby";
   return strtr(trim($chaine), $accent, $noaccent);
}

if (filter_input(INPUT_POST,'zonerecherche',FILTER_SANITIZE_ADD_SLASHES)!=="") {
   $zonerecherche = antiInjection(filter_input(INPUT_POST,'zonerecherche',FILTER_SANITIZE_ADD_SLASHES));
   $zonerecherche2 = trim($zonerecherche);
   $id = sprintf("%d", filter_input(INPUT_POST,'id',FILTER_SANITIZE_ADD_SLASHES));
   if (($zonerecherche2 != null && !empty($zonerecherche2)) && ($id != null && !empty($id))) {
      unset($sqlparam);
      $sqlparam["table"][] = "employes";
      $sqlparam["champs"][] = "nomfamille";
      $sqlparam["champs"][] = "prenom";
      $sqlparam["champs"][] = "numemploye";
      $sqlparam["champs"][] = "adresse1";
      $sqlparam["champs"][] = "adresse2";
      $sqlparam["champs"][] = "ville";
      $sqlparam["champs"][] = "codepostal";
      $sqlparam["champs"][] = "numtel";
      $sqlparam["champs"][] = "membreca";
      $sqlparam["champs"][] = "refemploye";
      $sqlparam["champs"][] = "presentation";
      $sqlparam["champs"][] = "email";
      $sqlparam["champs"][] = "numemploye";
      $sqlparam["order"][] = "nomfamille";
      $sqlparam["order"][] = "prenom";
      $sqlparam["where"][] = "TRIM(CONCAT_WS(' ',nomfamille,prenom)) LIKE '%" . trim($zonerecherche2) . "%'";
      if ($_SESSION['gestion_site'] == 'Non') {
         $sqlparam["where"][] = sprintf("employes.refemployeur=%d", $_SESSION['employeur']);
      }
      // Ne pas prendre les employés qui ont été archivés
      $sqlparam["where"][] = "archive=0";
      $sqlparam["limite"] = 7;

      $result = executerRequeteSql($sqlparam);
      if ($result) {
         foreach ($result as $clef => $champs) {
            $valeur0 = $champs["nomfamille"];
            $valeur1 = $champs["prenom"];
            $valeur2 = $champs["refemploye"];
            $valeur3 = $champs["numemploye"];
            $valeur4 = $champs["ville"];
            $ligne = enleveAccent(htmlentities($valeur0 . ", " . $valeur1 . " (" . $valeur3 . "), " . $valeur4, ENT_SUBSTITUTE, "UTF-8"));
            $zonerecheche = strtolower(enleveAccent($zonerecherche2));
            $ligne2 = ucwords($ligne);
            $ligne3 = str_ireplace(strtoupper($zonerecherche2), '<span class="highlight">' . strtoupper($zonerecherche2) . '</span>', $ligne2);
            $nomTemp = str_replace("'", "&apos;", htmlentities($valeur0 . ", " . $valeur1, ENT_SUBSTITUTE, "UTF-8"));
            print sprintf("<a style='cursor:pointer;' onclick='selectedC(%d,\"%s\",%d);'> %s</a>", $id, $nomTemp, $valeur2, $ligne3);
         }
      } else {
         print AUCUN_RESULTAT;
      }
   } else {
      print AUCUN_RESULTAT;
   }
}
